﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.Reflection;

namespace TFSWorkflowControl.TSWA.SL
{
    
    public partial class MainPage : UserControl
    {
        private int _currentIndex;
        private TFSWorkflowControl.TSWA.SL.RevisionCollection _revisionCollection;
        private string _currentField;

        public MainPage()
        {
            InitializeComponent();
            _revisionCollection = new TFSWorkflowControl.TSWA.SL.RevisionCollection();

            HtmlPage.RegisterScriptableObject("Workflow", this);
            HtmlPage.RegisterScriptableObject("RevisionCollection", _revisionCollection);

            var assembly = Assembly.GetExecutingAssembly();
            var assemblyName = new AssemblyName(assembly.FullName);
            versionLabel.Text = assemblyName.Version.ToString();    
        }

        [ScriptableMember]
        public void SetDefaultField(string field)
        {
            if (_currentField == null)
            {
                _currentField = field;
            }
        }

        [ScriptableMember]
        public void StartEditCollection()
        {
            _revisionCollection.RevisionList.Clear();

        }

        [ScriptableMember]
        public void EndEditCollection()
        {
         
            this.Dispatcher.BeginInvoke(() => UpdateUI());
        }

        public void UpdateUI()
        {
            
            if (_revisionCollection.FieldList.Count == 0)
            {

            }
            else
            {
                if (_currentField == null)
                {
                    _currentField = _revisionCollection.FieldList[0];
                    _currentIndex = 0;
                }
                var sic = new StateInfoCollection();
                sic.FillFromStateInfoCollection(_revisionCollection, _currentField);
                LayoutRoot.DataContext = sic;
                cbGroup.ItemsSource = _revisionCollection.FieldList;
                cbGroup.SelectedIndex = _currentIndex;
            }
          
        }

        private void cbGroup_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {           
            string newFieldSelection = cbGroup.SelectedItem as string;
            if (newFieldSelection == null || newFieldSelection == _currentField)
            {
                return;
            }
            _currentField = newFieldSelection;
            _currentIndex = cbGroup.SelectedIndex;
            var sic = new StateInfoCollection();
            sic.FillFromStateInfoCollection(_revisionCollection, _currentField);
            LayoutRoot.DataContext = sic;
        }
    }
}
